@extends('layouts.paymentForm')

@section('content')

<div class="od_box" id="payConfirm" style="display:none">
    <div class="od_body ">
        <h1>是否支付成功</h1>
        <p>请选择</p>
        <div class="overflow_h"><a onclick="checkPayment()" class="but03">已支付</a><a onclick="history.back()" class="but03">未支付</a></div>
    </div>
</div>
<script type="text/javascript">
    var trade_number = '{{$trade_number}}';
    var amount = '{{$amount}}';
    var payment_method = '{{$payment_method}}';
    var redirect_url = "{{$redirect_url}}";

    //检查是否支付成功
    checkPayment = _.throttle(function ()
    {

        var callback = window.callback || function(ret,message){
            alert(message);
            location.href = redirect_url;
        }

        axios.post('/ajax/pay/checkpaymentsuccess', {
            trade_number:trade_number,
            payment_method:payment_method,
        }).then(function (response) {
            if (response.data.code == 200) {
                callback(true,'支付成功');
            } else {
                callback(false,response.data.message || '支付失败');
            }
        },function (response) {
            callback(false,response.data.message || '支付失败');
        }).catch(function (err) {
            callback(false,'支付失败');
        });
    },1500);

    //调用微信JS api 支付
    function jsApiCall()
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            <?php echo $jsApiParameters; ?>,
            function(res){
                WeixinJSBridge.log(res.err_msg);
                if (res.err_msg == 'get_brand_wcpay_request:ok'){//支付成功
                    checkPayment();
                }else if (res.err_msg == 'get_brand_wcpay_request:cancel'){//支付过程中用户取消
                    location.href = redirect_url;
                }else if (res.err_msg == 'get_brand_wcpay_request:fail'){//支付失败
                    alert('支付失败');
                    location.href = redirect_url;
                }
//                $('#payConfirm').show();
            }
        );
    }

    function callpay()
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall();
        }
    }
</script>
<script type="text/javascript">

    /**
     * 调用提交支付方法
     */
    if (trade_number){
        callpay();
    }else{
        alert('系统出错，请重新再试');
        location.href = redirect_url;
    }

</script>

@endsection